import type { HttpContext } from '@adonisjs/core/http'

import BaseController from '#common/base_controller'
import StockOrderModel from '#models/stock_order'
import StockInService from '#services/stock_in_service'
import { createStockInValidator } from '#validators/stock'

export default class StockInController extends BaseController {
  async list(ctx: HttpContext) {
    return this.generatePageData(ctx, StockOrderModel)
  }

  async create(ctx: HttpContext) {
    const data = await ctx.request.validateUsing(createStockInValidator)
    return StockInService.create('RK', data)
  }

  async detail({ params }: HttpContext) {
    const stockInInfo = await StockOrderModel.findOrFail(params.orderId)
    await stockInInfo.load('stockInDetail')
    return stockInInfo
  }

  async update(ctx: HttpContext) {
    const data = await ctx.request.validateUsing(createStockInValidator)
    const orderId = ctx.request.param('orderId')
    return await StockInService.update(orderId, data)
  }

  async delete(ctx: HttpContext) {
    const orderId = ctx.request.param('orderId')
    return await StockInService.delete(orderId)
  }
}
